package com.haochang.chunkvod.controller.activity.hostess;

import java.util.ArrayList;
import java.util.Locale;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.ConsumeInfo;
import com.haochang.chunkvod.model.hostess.GoodsFeeInfo;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.tools.vod.other.AppendImageUtil;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

/**
 *         created at 2016/8/29 10:27
 *         消费详情页面
 */
public class ConsumeInfoActivity extends BaseActivity
{
	private BaseTextView tv_person, tv_money_avg, tv_start_time, tv_time, tv_room_money, total_money;
	private BaseListView consume_info_list;
	private BaseTextView tv_info_bar;//消费明细的bar,	未获取数据时隐藏
	private ArrayList<GoodsFeeInfo> listInfos = new ArrayList<GoodsFeeInfo>();
	private String billId = "";
	private String roomNum = "";
	private HostessData hostessData;
	private QuickAdapter<GoodsFeeInfo> adapter;

	@Override
	public void receiveParam()
	{
		billId = getIntent().getStringExtra(IntentKey.BILL_ID);
		roomNum = getIntent().getStringExtra(IntentKey.ROOM_NUM);
	}

	@Override
	public void initData()
	{
		hostessData = new HostessData(this);
	}

	@Override
	public void initViewsData()
	{
		super.initViewsData();
		tv_info_bar.setVisibility(View.GONE);
		adapter = new QuickAdapter<GoodsFeeInfo>(ConsumeInfoActivity.this, R.layout.item_consume_info_first, listInfos)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, GoodsFeeInfo item)
			{
				if (item != null)
				{
					BaseTextView text = helper.getView(R.id.tv_goods_money);
					text.setTextColor(item.is_give() ? getResources().getColor(R.color.color_f04e39) : getResources().getColor(R.color.color_282828));
					text.getPaint().setFlags(item.is_give() ? Paint.STRIKE_THRU_TEXT_FLAG : 0);
					if (item.is_give())
					{
						((BaseTextView) helper.getView(R.id.tv_goods_name)).append(AppendImageUtil.addImageSpan(context, R.drawable.ic_gift, 10));
						text.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.doller_red), null, null, null);
					}
					helper.setText(R.id.tv_goods_name, item.getGoodsName());
					helper.setText(R.id.tv_goods_money, String.valueOf(item.getMoney() / 100));
					helper.setText(R.id.tv_goods_count, item.getCount() + "");
				}
			}
		};
		consume_info_list.setAdapter(adapter);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_consume_info);
		TitleView titleView = (TitleView) findViewById(R.id.titleView);
		if (!TextUtils.isEmpty(billId) && !TextUtils.isEmpty(roomNum))
		{
			titleView.setTitle("消费详情(" + roomNum + ")").setRightIconVISIBLE(false).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
			{
				@Override
				public void onBackClick()
				{
					finish();
					animRightToLeft();
				}

				@Override
				public void onDoneClick()
				{

				}
			});
		}
		tv_person = (BaseTextView) findViewById(R.id.tv_person);
		tv_money_avg = (BaseTextView) findViewById(R.id.tv_money_avg);
		tv_start_time = (BaseTextView) findViewById(R.id.tv_start_time);
		tv_time = (BaseTextView) findViewById(R.id.tv_time);
		tv_room_money = (BaseTextView) findViewById(R.id.tv_room_money);
		total_money = (BaseTextView) findViewById(R.id.total_money);
		consume_info_list = (BaseListView) findViewById(R.id.consume_info_list);
		tv_info_bar = (BaseTextView) findViewById(R.id.tv_info_bar);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		requestConsumeInfo();
	}

	/**
	 * 获取账单具体一项 的详情
	 */
	private void requestConsumeInfo()
	{
		hostessData.requestConsumeInfo(billId, new HostessData.ConsumeInfoListener()
		{
			@Override
			public void onRequestSuccess(ConsumeInfo info)
			{
				if (info != null)
				{
					if (info.getGoodsFeeInfos() != null)
					{
						listInfos.clear();
						adapter.replaceAll(info.getGoodsFeeInfos());
					}
					if (null != listInfos && listInfos.size() > 0)
					{
						tv_info_bar.setVisibility(View.VISIBLE);//显示消费明细的bar
					}
					tv_person.setText(String.valueOf(info.getCustomer_count()));
					String avg = String.format(Locale.CHINA, "%.1f", info.getConsume() / info.getCustomer_count() / 100.0);//保留小数点后一位
					tv_money_avg.setText(info.getCustomer_count() > 0 ? avg + "元" : 0 + "元");
					tv_start_time.setText(TimeUtils.getMinutesString(info.getBegin_time()));
					tv_time.setText(TimeUtils.timeMinusCn(TimeUtils.getNowTime(), info.getBegin_time()));
					tv_room_money.setText(String.valueOf(info.getRoom_fee() / 100 + "元"));
					total_money.setText(String.valueOf(info.getConsume() / 100));
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				ToastUtils.showText(ConsumeInfoActivity.this, TextUtils.isEmpty(errMsg) ? "消费明细查询失败" : errMsg);
			}
		});
	}

}
